package org.example.music_manage_system.controller;


import org.example.music_manage_system.pojo.Result;
import org.example.music_manage_system.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String>upload(MultipartFile file) throws Exception {
        //文件的内容存储到本地磁盘,无法直接访问，且内存有限
        //OSS可以存储和调用图片，语音等资源
        //注册，softwareDevelop Kit,集成
        //bucket存储空间，AccessKey
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，防止相同文件被覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("D:\\MusicFiles\\"+filename));
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}